/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.pay;

import com.jfinal.config.Routes;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Directive;
import com.jfinal.template.Engine;
import com.lambkit.Lambkit;
import com.lambkit.common.service.ServiceManager;
import com.lambkit.core.rpc.RpcConfig;
import com.lambkit.db.datasource.ActiveRecordPluginWrapper;
import com.lambkit.module.LambkitModule;

import com.lambkit.module.pay.PayConfig;
import com.lambkit.module.pay.rpc.model.PayInOrder;
import com.lambkit.module.pay.rpc.model.PayInOrderDetail;
import com.lambkit.module.pay.rpc.model.PayMch;
import com.lambkit.module.pay.rpc.model.PayOutOrder;
import com.lambkit.module.pay.rpc.model.PayOutOrderDetail;
import com.lambkit.module.pay.rpc.model.PayPay;
import com.lambkit.module.pay.rpc.model.PayType;
import com.lambkit.module.pay.rpc.model.PayVendor;
import com.lambkit.module.pay.rpc.model.PayVest;
import com.lambkit.module.pay.rpc.api.PayInOrderService;
import com.lambkit.module.pay.rpc.api.PayInOrderDetailService;
import com.lambkit.module.pay.rpc.api.PayMchService;
import com.lambkit.module.pay.rpc.api.PayOutOrderService;
import com.lambkit.module.pay.rpc.api.PayOutOrderDetailService;
import com.lambkit.module.pay.rpc.api.PayPayService;
import com.lambkit.module.pay.rpc.api.PayTypeService;
import com.lambkit.module.pay.rpc.api.PayVendorService;
import com.lambkit.module.pay.rpc.api.PayVestService;
import com.lambkit.module.pay.service.impl.PayInOrderServiceImpl;
import com.lambkit.module.pay.service.impl.PayInOrderServiceMock;
import com.lambkit.module.pay.service.impl.PayInOrderDetailServiceImpl;
import com.lambkit.module.pay.service.impl.PayInOrderDetailServiceMock;
import com.lambkit.module.pay.service.impl.PayMchServiceImpl;
import com.lambkit.module.pay.service.impl.PayMchServiceMock;
import com.lambkit.module.pay.service.impl.PayOutOrderServiceImpl;
import com.lambkit.module.pay.service.impl.PayOutOrderServiceMock;
import com.lambkit.module.pay.service.impl.PayOutOrderDetailServiceImpl;
import com.lambkit.module.pay.service.impl.PayOutOrderDetailServiceMock;
import com.lambkit.module.pay.service.impl.PayPayServiceImpl;
import com.lambkit.module.pay.service.impl.PayPayServiceMock;
import com.lambkit.module.pay.service.impl.PayTypeServiceImpl;
import com.lambkit.module.pay.service.impl.PayTypeServiceMock;
import com.lambkit.module.pay.service.impl.PayVendorServiceImpl;
import com.lambkit.module.pay.service.impl.PayVendorServiceMock;
import com.lambkit.module.pay.service.impl.PayVestServiceImpl;
import com.lambkit.module.pay.service.impl.PayVestServiceMock;
import com.lambkit.module.pay.web.directive.PayInOrderDirective;
import com.lambkit.module.pay.web.directive.PayInOrderDetailDirective;
import com.lambkit.module.pay.web.directive.PayMchDirective;
import com.lambkit.module.pay.web.directive.PayOutOrderDirective;
import com.lambkit.module.pay.web.directive.PayOutOrderDetailDirective;
import com.lambkit.module.pay.web.directive.PayPayDirective;
import com.lambkit.module.pay.web.directive.PayTypeDirective;
import com.lambkit.module.pay.web.directive.PayVendorDirective;
import com.lambkit.module.pay.web.directive.PayVestDirective;
import com.lambkit.module.pay.web.tag.PayInOrderMarker;
import com.lambkit.module.pay.web.tag.PayInOrderDetailMarker;
import com.lambkit.module.pay.web.tag.PayMchMarker;
import com.lambkit.module.pay.web.tag.PayOutOrderMarker;
import com.lambkit.module.pay.web.tag.PayOutOrderDetailMarker;
import com.lambkit.module.pay.web.tag.PayPayMarker;
import com.lambkit.module.pay.web.tag.PayTypeMarker;
import com.lambkit.module.pay.web.tag.PayVendorMarker;
import com.lambkit.module.pay.web.tag.PayVestMarker;

/**
 * @author Henry 杨勇 (孤竹行) 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2019-11-05
 * @version 1.0
 * @since 1.0
 */
public class PayModule extends LambkitModule  {

	@Override
	public void configMapping(ActiveRecordPluginWrapper arp) {
		if(StrKit.isBlank(getConfig().getDbconfig())) {
			mapping(arp);
		}
	}
	
	@Override
	public void configMapping(String name, ActiveRecordPluginWrapper arp) {
		super.configMapping(name, arp);
		if(StrKit.notBlank(name) && name.equals(getConfig().getDbconfig())) {
			mapping(arp);
		}
	}
	
	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		me.addDirective("payInOrder", PayInOrderDirective.class);
		me.addDirective("payInOrderDetail", PayInOrderDetailDirective.class);
		me.addDirective("payMch", PayMchDirective.class);
		me.addDirective("payOutOrder", PayOutOrderDirective.class);
		me.addDirective("payOutOrderDetail", PayOutOrderDetailDirective.class);
		me.addDirective("payPay", PayPayDirective.class);
		me.addDirective("payType", PayTypeDirective.class);
		me.addDirective("payVendor", PayVendorDirective.class);
		me.addDirective("payVest", PayVestDirective.class);
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		addTag(this);
		if("server".equals(getConfig().getServerType())) {
			registerLocalService();
		} else if("client".equals(getConfig().getServerType())) {
			registerRemoteService();
		} 
	}
	
	public void mapping(ActiveRecordPluginWrapper arp) {
		arp.addMapping("pay_in_order", "pay_in_order_id", PayInOrder.class);
		arp.addMapping("pay_in_order_detail", "pay_in_order_detail_id", PayInOrderDetail.class);
		arp.addMapping("pay_mch", "pay_mch_id", PayMch.class);
		arp.addMapping("pay_out_order", "pay_out_order_id", PayOutOrder.class);
		arp.addMapping("pay_out_order_detail", "pay_out_order_detail_id", PayOutOrderDetail.class);
		arp.addMapping("pay_pay", "pay_pay_id", PayPay.class);
		arp.addMapping("pay_type", "pay_type_id", PayType.class);
		arp.addMapping("pay_vendor", "pay_vendor_id", PayVendor.class);
		arp.addMapping("pay_vest", "pay_vest_id", PayVest.class);
	}
	
	public void addTag(LambkitModule lk) {
		lk.addTag("payInOrder", new PayInOrderMarker());
		lk.addTag("payInOrderDetail", new PayInOrderDetailMarker());
		lk.addTag("payMch", new PayMchMarker());
		lk.addTag("payOutOrder", new PayOutOrderMarker());
		lk.addTag("payOutOrderDetail", new PayOutOrderDetailMarker());
		lk.addTag("payPay", new PayPayMarker());
		lk.addTag("payType", new PayTypeMarker());
		lk.addTag("payVendor", new PayVendorMarker());
		lk.addTag("payVest", new PayVestMarker());
	}
			
	public void registerLocalService() {
		registerLocalService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerLocalService(String group, String version, int port) {
		ServiceManager.me().mapping(PayInOrderService.class, PayInOrderServiceImpl.class, PayInOrderServiceMock.class, group, version, port);
		ServiceManager.me().mapping(PayInOrderDetailService.class, PayInOrderDetailServiceImpl.class, PayInOrderDetailServiceMock.class, group, version, port);
		ServiceManager.me().mapping(PayMchService.class, PayMchServiceImpl.class, PayMchServiceMock.class, group, version, port);
		ServiceManager.me().mapping(PayOutOrderService.class, PayOutOrderServiceImpl.class, PayOutOrderServiceMock.class, group, version, port);
		ServiceManager.me().mapping(PayOutOrderDetailService.class, PayOutOrderDetailServiceImpl.class, PayOutOrderDetailServiceMock.class, group, version, port);
		ServiceManager.me().mapping(PayPayService.class, PayPayServiceImpl.class, PayPayServiceMock.class, group, version, port);
		ServiceManager.me().mapping(PayTypeService.class, PayTypeServiceImpl.class, PayTypeServiceMock.class, group, version, port);
		ServiceManager.me().mapping(PayVendorService.class, PayVendorServiceImpl.class, PayVendorServiceMock.class, group, version, port);
		ServiceManager.me().mapping(PayVestService.class, PayVestServiceImpl.class, PayVestServiceMock.class, group, version, port);
	}
	
	public void registerRemoteService() {
		registerRemoteService(getRpcGroup(), getRpcVersion(), getRpcPort());
	}
	
	public void registerRemoteService(String group, String version, int port) {
		ServiceManager.me().remote(PayInOrderService.class, PayInOrderServiceMock.class, group, version, port);
		ServiceManager.me().remote(PayInOrderDetailService.class, PayInOrderDetailServiceMock.class, group, version, port);
		ServiceManager.me().remote(PayMchService.class, PayMchServiceMock.class, group, version, port);
		ServiceManager.me().remote(PayOutOrderService.class, PayOutOrderServiceMock.class, group, version, port);
		ServiceManager.me().remote(PayOutOrderDetailService.class, PayOutOrderDetailServiceMock.class, group, version, port);
		ServiceManager.me().remote(PayPayService.class, PayPayServiceMock.class, group, version, port);
		ServiceManager.me().remote(PayTypeService.class, PayTypeServiceMock.class, group, version, port);
		ServiceManager.me().remote(PayVendorService.class, PayVendorServiceMock.class, group, version, port);
		ServiceManager.me().remote(PayVestService.class, PayVestServiceMock.class, group, version, port);
	}
	
	public int getRpcPort() {
		return Lambkit.config(RpcConfig.class).getDefaultPort();
	}
	public String getRpcGroup() {
		return Lambkit.config(RpcConfig.class).getDefaultGroup();
	}
	public String getRpcVersion() {
		return getConfig().getVersion();
	}
	public PayConfig getConfig() {
		return Lambkit.config(PayConfig.class);
	}
}

